<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="name"
        @keyup.enter.native="search"
      ></el-input>
      <el-button @click="resetName">清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80" :index="indexMethod">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="updataTask(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delTask(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        small
        layout="prev, pager, next"
        :page-size="5"
        :total="total"
        @current-change="handleCurrentChange"
      />
      <el-button size="small" plain @click="openTaskDialog">+ 新增</el-button>
    </div>
    <!-- 弹窗 -->
    <el-dialog
      :title="formData.id ? '编辑任务' : '添加任务'"
      :visible="taskDialog"
      @close="closeTaskDialog"
      width="450px"
    >
      <el-form
        :model="formData"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="formData.des"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeTaskDialog">取 消</el-button>
          <el-button type="primary" @click="publish">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTaskListAPI,
  addTaskAPI,
  getTaskAPI,
  updataTaskAPI,
  delTaskAPI,
  searchTaskAPI
} from '@/api/task.js'
import { v4 as uuisv4 } from 'uuid'
export default {
  data() {
    return {
      // 请求数据
      query: {
        _page: 1,
        _limit: 5
      },
      // 列表数据
      list: [],
      // 列表总数
      total: 0,
      taskDialog: false,
      formData: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
        des: [{ required: true, message: '请输入任务描述', trigger: 'blur' }]
      },
      name: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      const res = await getTaskListAPI(this.query)
      this.list = res.data
      this.total = Number(res.headers['x-total-count'])
    },
    // 改变页码
    handleCurrentChange(value) {
      // console.log(value);
      this.query._page = value
      this.getList()
    },
    // 序号格式化
    indexMethod(value) {
      return value + 1 + (this.query._page - 1) * this.query._limit
    },
    // 关闭表单
    closeTaskDialog() {
      this.taskDialog = false
      this.$refs.ruleForm.resetFields()
      this.formData = {
        name: '',
        des: '',
        id: ''
      }
    },
    // 打开弹窗获取id
    openTaskDialog() {
      this.taskDialog = true
    },
    // 提交数据
    publish() {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return
        let res = {}
        if (this.formData.id) {
          res = await updataTaskAPI(this.formData)
        } else {
          this.formData.id = uuisv4()
          res = await addTaskAPI(this.formData)
        }
        if (res.status === 201 || res.status === 200) {
          this.$message.success(`${res.status === 200 ? '编辑' : '添加'}成功`)
          this.closeTaskDialog()
          this.getList()
        }
      })
    },
    // 获取原数据
    async updataTask(id) {
      this.taskDialog = true
      const { data: res } = await getTaskAPI(id)
      this.formData = res
    },
    // 删除任务
    async delTask(id) {
      const res = await delTaskAPI(id)
      // console.log(res);
      if (res.status === 200) {
        this.$message.success('删除成功')
        this.getList()
      }
    },
    // 清空搜索框
    resetName() {
      this.name = ''
      this.getList()
    },
    // 搜索功能
    async search() {
      if (this.name === '') return alert('请输入搜索内容')
      const res = await searchTaskAPI(this.name)
      // console.log(res);
      this.list = res.data
      this.total = Number(res.headers['x-total-count'])
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
